﻿// https://leetcode-cn.com/problems/find-bottom-left-tree-value/
// 513. 找树左下角的值

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        int result;
        queue<TreeNode*> que;

        if (root != nullptr) que.push(root);

        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i ++) {
                TreeNode* front = que.front();
                que.pop();
                if (i == 0) result = front->val;
                if (front->left) que.push(front->left);
                if (front->right) que.push(front->right);
            }
        }

        return result;
    }
};

int main() {
    Solution obj = Solution();
}